package com.weixing.mall.provider.controller.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.erp.vo.ResultStyleInfoVo;
import com.weixing.mall.provider.api.model.vo.FrontGoodsVo;
import com.weixing.mall.provider.erp.service.ErpService;
import com.weixing.mall.provider.model.domain.Goods;
import com.weixing.mall.provider.model.vo.SpuVo;
import com.weixing.mall.provider.service.IGoodsService;
import com.weixing.mall.provider.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品通用Api
 *
 * @author Small
 * @date 2019/11/5 18:05
 * @since 1.0
 **/
@RestController
@RequestMapping("common/goods")
public class CommonGoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ErpService erpService;

    @Autowired
    private IProductService productService;

    /**
     * 根据goodsSn去查找正价商品
     *
     * @param goodsSn   商品码
     * @return 商品信息
     */
    @GetMapping(value = "goodsSn/{goodsSn}")
    @NoNeedAccess
    public Result<Page<FrontGoodsVo>> loadItemByGoosSn(@PathVariable String goodsSn) {
        //加载数据
        Page<FrontGoodsVo> spuVo = goodsService.loadModelByGoodsSn(initMpPage(), goodsSn);
        return ResultUtil.success(spuVo);
    }

    /**
     * 验证goodsSn是否已存在
     * @param goodsSn   商品码
     * @return 是否存在
     */
    @GetMapping(value = "checkGoodsSn/{goodsSn}")
    @NoNeedAccess
    public Result<Page<FrontGoodsVo>> checkGoodsSn(@PathVariable String goodsSn) {
        //加载数据
        Goods goods = goodsService.selectByGoodsSn(goodsSn);
        if (null == goods) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * 验证styleNo是否已存在
     * @param styleNo   款号
     * @return 是否存在
     */
    @GetMapping(value = "checkStyleNo/{styleNo}")
    @NoNeedAccess
    public Result<Page<FrontGoodsVo>> checkStyleNo(@PathVariable String styleNo) {
        //加载数据
        Goods goods = goodsService.selectByStyleNo(styleNo);
        if (null == goods) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * 验证styleNoERP同步返回的数据
     *
     * @param styleNo      款号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return 结果
     */
    @GetMapping(value = "checkErpStyleNo")
    @NoNeedAccess
    public Result<ResultStyleInfoVo> checkByStyleNo(String styleNo, String startTime, String endTime) {
        //加载数据
        ResultStyleInfoVo resultStyleInfoVo = erpService.checkByStyleNo(styleNo, startTime, endTime);

        return ResultUtil.success(resultStyleInfoVo);
    }

    /**
     * 根据商品id查询商品详情
     * @param id   商品id
     * @return 商品详情
     */
    @GetMapping(value = "{id}")
    public Result<SpuVo> item(@PathVariable Long id) {
        SpuVo spuVo = this.goodsService.loadModel(id, null, null);
        return ResultUtil.success(spuVo);
    }

    /**
     * 商品清晰数据
     * @return
     */
    @GetMapping()
    @NoNeedAccess
    public Result<SpuVo> item() {
        productService.getCheckAll();
        return ResultUtil.success();
    }


}
